# Contributor: Matthias Ahouansou <matthias@ahouansou.cz>
# Maintainer: Matthias Ahouansou <matthias@ahouansou.cz>
pkgname=mautrix-signal
pkgver=0.4.3_git20231218
_gitrev=947575a5b094a9d7999d978e1803b5ec2628890d
_libsignalver=0.36.1
pkgrel=0
pkgdesc="A Matrix-Signal puppeting bridge"
url="https://go.mau.fi/mautrix-signal/"
arch="x86_64 aarch64" # x86 blocked by boring ssl, everything else by cpufeatures
license="AGPL-3.0-or-later"
makedepends="
	cargo
	cargo-auditable
	clang-libclang
	cmake
	go
	olm-dev
	protoc
	sqlite-dev
"
install="$pkgname.pre-install"
subpackages="$pkgname-openrc $pkgname-doc"
source="
	signal-$_gitrev.tar.gz::https://github.com/mautrix/signal/archive/$_gitrev.tar.gz
	libsignal-$_libsignalver.tar.gz::https://github.com/signalapp/libsignal/archive/refs/tags/v$_libsignalver.tar.gz
	mautrix-signal.initd
	mautrix-signal.confd
	default-log-dir.patch
"
builddir="$srcdir/signal-$_gitrev"
options="net"
_rust_dir=pkg/libsignalgo/libsignal

export GOFLAGS="$GOFLAGS -tags=libsqlite3"
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare
	mv -T "$srcdir/libsignal-$_libsignalver" "$_rust_dir"
	cd "$_rust_dir"
	cargo fetch --target="$CTARGET" --locked
}

build() {
	export CGO_CFLAGS="$CFLAGS"
	export CGO_LDFLAGS="$LDFLAGS"
	cd "$_rust_dir"
	cargo auditable build -p libsignal-ffi --release --frozen
	mv target/release/libsignal_ffi.a "$builddir"
	cd "$builddir"
	LIBRARY_PATH=. go build -ldflags "-X main.Tag=$pkgver -X main.Commit=$_gitrev -X 'main.BuildTime=$(date '+%b %_d %Y, %H:%M:%S')'"
}

check() {
	go test ./...
}

package() {
	install -Dm755 mautrix-signal \
		-t "$pkgdir"/usr/bin/
	install -Dm644 example-config.yaml \
		-t "$pkgdir"/etc/mautrix-signal/

	install -Dm755 "$srcdir"/mautrix-signal.initd \
		"$pkgdir"/etc/init.d/mautrix-signal
	install -Dm644 "$srcdir"/mautrix-signal.confd \
		"$pkgdir"/etc/conf.d/mautrix-signal

	install -Dm 644 LICENSE "$pkgdir"/usr/share/licenses/"$pkgname"/LICENSE
}

sha512sums="
e9908fe0f9bd1bb3c425e6292efd22dad1660b6eef69f0df6f3dcd1ca8deeff5c317b2e1539bf5cff76aef4dc3e3df5e0bf2e6eb1da3cc744856f7560cd6f18c  signal-947575a5b094a9d7999d978e1803b5ec2628890d.tar.gz
1131c101c8899258078f8a55a1754e0bda72b2a2551cd77bd1444a51cb8cc1f3b1d06aeb0ddaaefc628f8c9635378b8348ffc6c531b40cba1facaeb48399dd33  libsignal-0.36.1.tar.gz
90555c016fc9d9aa90b5115b898c3e2f365d277390862fa6de693970db53d6ae41e0e55ca1ca5315d52fc14f61a58e7d8713aefd4eb4050241cfdbb27f9a8213  mautrix-signal.initd
ffcfed000e4ca779a055ddf4374753e23e424501a70a148e9785a4c4f6f2d32e94e2275a766ac9435b2509a1d621821a2baad8c5e4964ef08cbd13045cb430df  mautrix-signal.confd
8c355de5a4a6714afa2aec594f9035b8cb25fab153e83a2222a17f2068d10a970f9ef2e1854085a122494df152831dfcdeb5612f648dc685fe6d9c0b4b4107f4  default-log-dir.patch
"
